﻿using System;
using System.Collections.Generic;
using System.Text;
using Yeti.MMedia;
using System.Collections;
using WaveLib;
using System.IO;
using Ripper;

namespace CD2WebAudio3Lib
{
    public delegate void WaveProgressHandler(object pSender, decimal pPercentDone);

    public class wave_class
    {
        private WaveWriter m_Writer = null;

        public event WaveProgressHandler WaveProgress;

        public bool Cancel = false;

        public bool Rip_To_TempFile(string pTempFileName, ArrayList pTracks, string pDriveLetter)
        {

            if (pTracks.Count > 0)
            {
                try
                {
                    CDDrive cddrive = main_functions.Get_CDDrive(pDriveLetter);
                    cddrive.LockCD();
                    try
                    {
                        WaveFormat Format = new WaveFormat(44100, 16, 2);

                        Stream WaveFile = new FileStream(pTempFileName, FileMode.Create, FileAccess.Write);
                        try
                        {
                            m_Writer = new WaveWriter(WaveFile, Format/*, pDrive.TrackSize(track)*/);
                            try
                            {
                                DateTime InitTime = DateTime.Now;
                                if (cddrive.ReadCD(pTracks, new CdDataReadEventHandler(writeWaveData), 0, 0, new CdReadProgressEventHandler(this.CdReadProgress)) > 0)
                                {
                                    TimeSpan Duration = DateTime.Now - InitTime;
                                    double Speed = cddrive.TotalBytes(pTracks) / Duration.TotalSeconds / Format.nAvgBytesPerSec;
                                }
                                else
                                {
                                    m_Writer.Close();
                                    WaveFile.Close();
                                }
                                if (Cancel)
                                {
                                    return false;
                                }
                            }
                            finally
                            {
                                m_Writer.Close();
                                m_Writer = null;
                            }
                        }
                        finally
                        {
                            WaveFile.Close();
                        }
                    }
                    finally
                    {
                        cddrive.UnLockCD();
                    }
                }
                finally
                {
                    //m_Ripping = false;
                }

            }

            return true;

        }

        private void writeWaveData(object sender, DataReadEventArgs ea)
        {
            if (m_Writer != null)
            {
                m_Writer.Write(ea.Data, 0, (int)ea.DataSize);
            }
        }

        private void CdReadProgress(object sender, ReadProgressEventArgs ea)
        {
            // to avoid divide-by-zero error
            if (ea.Bytes2Read.CompareTo((ulong)0) == 0) return;

            decimal percent = (decimal)ea.BytesRead * (decimal)100 / (decimal)ea.Bytes2Read;
            WaveProgress(this, percent);
            System.Windows.Forms.Application.DoEvents();
            ea.CancelRead |= this.Cancel;
        }


    }
}
